<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = $_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];


$info = array();
$id = 0;
if(!Check::isEmpty($_POST["id"])) {
    if(Check::isNum($_POST["id"]) && (int)$_POST["id"]>0) {
        $id = (int)$_POST["id"];
    }else {
        exit(Result::jsonReturn(1, "参数格式错误"));
    }
}else {
    exit(Result::jsonReturn(1, "参数不足"));
}
if(!Check::isEmpty($_POST["name"])) {
    if(Check::isName($_POST["name"])) {
        $info["name"] = $_POST["name"];
    }else {
        exit(Result::jsonReturn(1, "名称格式有误"));
    }    
}
if(!Check::isEmpty($_POST["slug"])) {
    if(Check::isSlug($_POST["slug"])) {
        $info["slug"] = $_POST["slug"];
    }else {
        exit(Result::jsonReturn(1, "缩略名格式错误"));
    }
}
if(!Check::isEmpty($_POST["description"])) {
        $info["description"] = Check::checkPost($_POST["description"]);
}
if(!Check::isEmpty($_POST["type"]) && ((int)$_POST["type"]==1||(int)$_POST["type"]==0)) {
    $info["type"] = (int)$_POST["type"];
}
if(count($info) == 0) {
    exit(Result::jsonReturn(1, "无更新内容"));
}

$sql = "UPDATE qilian_lists SET ";
foreach($info as $key=>$value) {
    if($key=="type") {
        $sql = $sql."{$key}={$value},";
    }else {
        $sql = $sql."{$key}='{$value}',";        
    }
}
$sql = Func::removeComma($sql);
$sql = $sql." WHERE id={$id}";
$db = new DB();
if($db->update($sql)) {
    $sql_sel = "SELECT * FROM qilian_lists WHERE id={$id}";
    $res = array();
    if($res = $db->getOne($sql_sel)) {
        $res['content'] = getCont($id, $db);
        exit(Result::jsonReturn(0, "更新成功", $res));        
    }else {
        exit(Result::jsonReturn(2, "获取更新信息失败"));
    }
}else {
    exit(Result::jsonReturn(1, "更新失败"));
}

function getCont($id, $db) {
    $sql = "SELECT content_id FROM qilian_relationships WHERE list_id={$id}";
    $res = $db->getAll($sql);
    $ret = array();
    foreach($res as $val) {
        array_push($ret, $val['content_id']);
    }
    return $ret;
}

?>